package demon1;

public class MyLinkedList implements IMylinkedList{

    static class ListNode{
        public int val;
        public ListNode prev;  //前驱
        public ListNode next;   //后继

        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;
    public ListNode last;
    public void check(int index){
        if(index<0||index>size()){
            throw new IndexExpection(index+"位置不合法");
        }
    }
    public ListNode findIndex(int index){
        ListNode cur=head;
        int count =0;
        while(count!=index){
            cur=cur.next;
            count++;
        }
        return cur;
    }
    @Override
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
        if(head==null){
            head=last=node;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node =new ListNode(data);
        if(head==null){
            head=last=node;
        }else{
            last.next=node;
            node.prev=last;
            last=node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        try{
            check(index);
        }catch (IndexExpection e){
            e.printStackTrace();
        }
        ListNode node=new ListNode(data);
        if(index==0){
            addFirst(data);
        }
        if(index==size()){
            addLast(data);
        }
        ListNode cur=findIndex(index);
        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;

    }

    @Override
    public boolean contains(int key) {
        ListNode cur =head;
        while(cur!=null){
            if(cur.val==key){
                return false;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }else{
                        last=null;
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur==last){
                        last=last.prev;
                    }else{
                        cur.next.prev=cur.prev;
                    }
                    return;
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }else{
                        last=null;
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur==last){
                        last=last.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur=head;
        int count =0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

    @Override
    public void clear() {

    }
}
